/**
 * 执行全部用例
 */
import fs from 'fs';
import path from 'path';

import { taskCenter } from '../plugins/task-builder/test-task-center';

let failedCasesFirstTime: string[] = [];
describe('all case test', () => {
  // 使用时需指定对应的sdk目录
  // process.env['DEVECO_SDK_HOME'] = 'D:\\workSpace\\Deveco Studio\\DevEco Studio 5.0.3.522\\sdk';

  /* 此处 caseName 可以：
    1. 传【单个用例名】执行【单个】用例
    2. 多个用例名组成的【数组】执行【多个】用例
    或不传 caseName对象 而是：
    1. 写【筛选函数】执行【多个】用例
   */

  /**
   * 20250313更新全量黑名单方式，命令行传参为黑名单taskNames，执行e2e:all脚本
   */
  // 定义要写入的文件名（同级目录下）
  const filename = 'failedCases.txt';
  const targetFilePath = path.join(__dirname, filename);

  const args = process.argv.slice(2);
  taskCenter.runTask({
    caseName: (name) => !args.includes(name)
  }).then(rerunFailedCases => {
    failedCasesFirstTime = [...rerunFailedCases.map(c => c.caseName)];
    fs.writeFileSync(targetFilePath, failedCasesFirstTime.join('\n'), 'utf-8');
    if (failedCasesFirstTime.length > 0){
      console.log(`First time failed tests: \n ${rerunFailedCases.map(c => `${c.caseName},reason:${c.error})\n`)}`);
    } else {
      console.log('First e2e test passed');
    }
  });
});
